﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Modeling_TeacherPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WelcomeUser"] != null)
        {
            if (Session["EmployeeID"] != null && Session["Role"].ToString().Equals("EAS"))
            {
            }
            else
            {
                Response.Redirect("~/LoginMessage.aspx?id=1");
            }
        }
        else
        {
            Response.Redirect("~/Login.aspx?id=2");
        }
    }
    protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string pass = txtConfirmPassword.Text;

        if (pass.Length >= 6 && pass.Length <= 128)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string pass = txtNewPassword.Text;

        if (pass.Length >= 6 && pass.Length <= 128)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        GenerateDates gd = new GenerateDates();

        string username = Session["WelcomeUser"].ToString();
        string password = gd.EncodePassword(txtOldPassword.Text);

        if (udm.login(udm.createConnection(), 2, username, password) != null)
        {
            DataSet result = udm.login(udm.createConnection(), 2, username, password);

            if (result.Tables[0].Rows.Count > 0)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
        else
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
    protected void btnChange_Click(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        GenerateDates gd = new GenerateDates();

        string username = Session["WelcomeUser"].ToString();
        int userID = Convert.ToInt32(Session["EmployeeID"].ToString());
        string password = gd.EncodePassword(txtOldPassword.Text);
        string newPassword = gd.EncodePassword(txtNewPassword.Text);

        if (udm.login(udm.createConnection(), 2, username, password) != null)
        {
            DataSet result = udm.login(udm.createConnection(), 2, username, password);

            if (result.Tables[0].Rows.Count > 0 && txtConfirmPassword.Text.Length >= 6 && txtConfirmPassword.Text.Length <= 128 && txtNewPassword.Text.Length >= 6 && txtNewPassword.Text.Length <= 128)
            {
                if (!udm.resetPasswordEmployee(udm.createConnection(), userID, newPassword))
                {
                    Response.Redirect("~/ErrorPage.aspx");
                }
                else
                {
                    Response.Redirect("~/LoginMessage.aspx?id=4");
                }
            }
        }
        else
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
}